import { Component, OnInit } from '@angular/core';

declare var saveAs: any;
declare var form: any;
declare var base: any;

@Component({
  templateUrl: './build.web.html'
})
export class BuildWebComponent implements OnInit {
  fs: any = null;
  bfs: any = null;

  type = 1;
  proName: any = 'itfreer-xx';

  constructor() {
    this.fs = new form.FormTreeService('directory', {
      orderObject: { order: 0 },
      keyField: 'id',
      randomFields: ['id'],
      pageSize: 1000,
      defaultQueryObject: { 'directoryType:in': '1,3' }
    });
    // 加载表单数据
    this.fs.loadData();

    // 构建表单
    this.bfs = new form.FormTreeService('generate', {
      keyField: 'id'
    });
  }

  ngOnInit(): void {
  }

  start(mr, type) {
    this.type = type;
    this.bfs.addRow();
    this.bfs.er.mateID = mr.id;
    this.bfs.er.formAliseName = mr.display;
    this.bfs.er.fileName = mr.id.replace(/_/g, '.').toLowerCase();
    this.bfs.er.formName = mr.id.replace(/_/g, '').toLowerCase();
    this.bfs.er.keyFiled = 'id';
    this.bfs.er.order = 'id';
  }

  build() {
    let url = base.BaseService.getHostUrl() + '/generate/web/';
    if (this.type === 1) {
      url += 'simple';
    } else if (this.type === 2) {
      url += 'tree';
    } else if (this.type === 3) {
      url += 'sidesimple';
    } else if (this.type === 4) {
      url += 'sidetree';
    } else if (this.type === 5) {
      url += 'big';
    } else if (this.type === 6) {
      url += 'bpm';
    }
    this.bfs.isSaveing = true;
    this.bfs.er.proName = this.proName;
    base.BaseService.postSubscribe(url, this.bfs.er, (res: any) => {
      saveAs(res.body, this.bfs.er.formAliseName + '.zip');
      this.bfs.isSaveing = false;
      this.bfs.closeEdit();
    });
  }
}
